package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SwsSettingUpdDomainsController")
public class SwsSettingDomainUpdController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    // 修改域名
    @ApiOperation(value = "修改域名")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/setting/domain/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String domainId = reqModel.domainId;
        String domainName = reqModel.domainName;
        String suffix = reqModel.suffix;
        domainName = domainName.toLowerCase();
        domainName = domainName + suffix;

        // 判断域名是否存在
        int count = swsSettingDomainsRepository.countByDomainName(domainName);
        Assert.isTrue(count == 0, "当前域名已经被使用");

        SwsSettingDomains domains = swsSettingDomainsRepository.findById(domainId);
        Assert.isTrue(null != domains, "未获取到当前域名对象");
        domains.setDomainName(domainName);
        swsSettingDomainsRepository.save(domains);
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String domainId;
        public String domainName;
        public String suffix;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(domainId), "修改域名为空");
            Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
            Assert.isTrue(StringUtils.isNotBlank(suffix), "域名后缀为空");
            Assert.isTrue(!domainName.startsWith("www") && !domainName.startsWith("http"), "网站域名格式错误");
            Assert.isTrue(!domainName.endsWith(".cn") && !domainName.endsWith(".com"), "网站域名格式错误");
            Pattern p1 = Pattern.compile("^[a-zA-Z]{1}[a-zA-Z0-9]{2,19}$");
            Matcher m1 = p1.matcher(domainName);
            boolean b1 = m1.matches();
            Assert.isTrue(b1, "网站域名格式错误");
            Assert.isTrue(domainName.length() > 2 && domainName.length() < 31, "请输入3-20个字符的域名");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
